package com.cctc.services.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cctc.security.dao.UserDao;
import com.cctc.security.services.UserService;

public class Readfile
{
    static final String SQL_INERT = "INSERT INTO INFO(NAME,ACCOUNT, EMAIL) VALUES";
    static ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    static UserService userServices = (UserService) context.getBean("userService");
    static UserDao userDao = (UserDao) context.getBean("userDao");
    
    public static void main(String[] args)
    {
        File file = new File("F:\\CSDN\\wwwcsdnnet.sql");
        if (!file.exists() || file.isDirectory())
            try
            {
                throw new FileNotFoundException();
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
        BufferedReader br;
        try
        {
            br = new BufferedReader(new FileReader(file));
            String temp = "";
            temp = br.readLine();
            int i = 0;
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.append(SQL_INERT);
            while (temp != null)
            {
                String[] strs = temp.split("#");
                if (i != 1000)
                {
                    sqlBuilder.append("(").append("'").append(strs[0].trim()).append("'").append(",").append("'")
                            .append(strs[1].trim()).append("'").append(",").append("'").append(strs[2].trim())
                            .append("'").append(")").append(",");
                }
                else
                {
                    i = 0;
                    sqlBuilder.append("(").append("'").append(strs[0].trim()).append("'").append(",").append("'")
                            .append(strs[1].trim()).append("'").append(",").append("'").append(strs[2].trim())
                            .append("'").append(")");
                    try
                    {
                        userDao.add(sqlBuilder.toString());
                    }
                    catch (Exception e)
                    {
                        System.out.println(sqlBuilder);
                    }
                    
                    sqlBuilder = new StringBuilder();
                    sqlBuilder.append(SQL_INERT);
                }
                i++;
                temp = br.readLine();
            }
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
}
